/**
 * @file     longestPalindrome.ts
 * @brief    [409. 最长回文串](https://leetcode.cn/problems/longest-palindrome/description/)
 * @author   Zhu
 * @date     2022-11-02 23:22
 */

function longestPalindrome(s: string): number {
    // 字母出现次数: 偶数之和 + 奇数-1
    const map = new Map<string, number>();
    for (let i = 0; i < s.length; i++) {
        if (map.has(s[i])) {
            map.set(s[i], map.get(s[i])! + 1);
        } else {
            map.set(s[i], 1);
        }
    }

    let sum = 0;
    let hasOdd = false;
    for (const [key, count] of map) {
        if (count % 2 === 0) {
            sum += count;
        } else {
            hasOdd = true;
            sum += count - 1;
        }
    }

    return sum + (hasOdd ? 1 : 0);
}

function longestPalindrome(s: string): number {
    // 字母出现次数: 偶数之和 + 奇数-1
    const map = new Map<string, number>();
    for (let i = 0; i < s.length; i++) {
        if (map.has(s[i])) {
            map.set(s[i], map.get(s[i])! + 1);
        } else {
            map.set(s[i], 1);
        }
    }

    let oddNum = 0;
    for (const [key, count] of map) {
        if (count % 2 === 1) {
            oddNum++;
        }
    }

    return s.length - oddNum + (oddNum > 0 ? 1 : 0);
}
